package com.example.makerspace.manager;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.ContactInfoDAO;
import com.example.makerspace.domain.ContactInfoDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;


@Component
public class ContactInfoManager extends BaseManager<ContactInfoDO, ContactInfoDTO>{

    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "username").put(QUERY_TYPE, QUERY_TYPE_TERM).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "name").put(QUERY_TYPE, QUERY_TYPE_TERM).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "contact").put(QUERY_TYPE, QUERY_TYPE_TERM).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "email").put(QUERY_TYPE, QUERY_TYPE_TERM).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "phone").put(QUERY_TYPE, QUERY_TYPE_TERM).build()
    );
    @Resource
    private ContactInfoDAO contactInfoDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = contactInfoDAO;
        managerMap.put("contactInfo", this);
    }

    public Result addContactInfo(ContactInfoDTO contactInfoDTO){
        ContactInfoDO oldContactInfoDO = contactInfoDAO.selectDupInfo(contactInfoDTO);
        if(oldContactInfoDO != null){
            return Result.error(ResultCodeEnum.CONTACT_INFO_EXIST);
        }
        return addDO(contactInfoDTO);
    }

    public Result updateContactInfo(ContactInfoDTO contactInfoDTO){
        ContactInfoDO contactInfoDO = contactInfoDAO.selectById(contactInfoDTO.getId());
        if(contactInfoDO == null){
            return Result.error(ResultCodeEnum.CONTACT_INFO_NOT_EXIST);
        }
        Optional.ofNullable(contactInfoDTO.getUsername()).ifPresent(contactInfoDO::setUsername);
        Optional.ofNullable(contactInfoDTO.getRefType()).ifPresent(contactInfoDO::setRefType);
        Optional.of(contactInfoDTO.getRefId()).ifPresent(contactInfoDO::setRefId);
        Optional.of(contactInfoDTO.getRefName()).ifPresent(contactInfoDO::setRefName);
        Optional.ofNullable(contactInfoDTO.getName()).ifPresent(contactInfoDO::setName);
        Optional.ofNullable(contactInfoDTO.getPhone()).ifPresent(contactInfoDO::setPhone);
        Optional.ofNullable(contactInfoDTO.getContact()).ifPresent(contactInfoDO::setContact);
        Optional.ofNullable(contactInfoDTO.getEmail()).ifPresent(contactInfoDO::setEmail);
        return updateDO(contactInfoDO);
    }

    public Result contactDetail(ContactInfoDTO contactInfoDTO) {
        ContactInfoDO contactInfoDO = contactInfoDAO.selectByRef(contactInfoDTO);
        return Result.success(convert2DTO(contactInfoDO));
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        if(StringUtils.isBlank(fuzzy)){
            return super.list(pageRequest);
        }
        return search(null, null, FUZZY_QUERY_PARAMS, fuzzy, pagination, ContactInfoDO.class);
    }
}
